package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.PageList;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserOpenPlatformInfoDao;
import com.frogs.ucenter.entity.UserOpenPlatformInfo;
import com.frogs.ucenter.v1.request.UserOpenPlatformInfoAddRequest;
import com.frogs.ucenter.v1.request.UserOpenPlatformInfoListRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.PageListResponse;
import com.frogs.ucenter.v1.service.UserOpenPlatformInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION+"userOpenPlatformInfoService")
public class UserOpenPlatformInfoServiceImpl implements UserOpenPlatformInfoService{

	private static final Logger log = LoggerFactory.getLogger(UserOpenPlatformInfoServiceImpl.class);
	
	@Resource
	private UserOpenPlatformInfoDao userOpenPlatformInfoDao;
	
	/**
	 * 添加用户开发平台信息
	 */
	@Override
	public Response insert(Request request) throws Exception  {
		AddResponse<UserOpenPlatformInfo> response = new AddResponse<UserOpenPlatformInfo>();
		try {
			UserOpenPlatformInfoAddRequest userOpenPlatformInfoAddRequest = (UserOpenPlatformInfoAddRequest) request;
			Integer userId = userOpenPlatformInfoAddRequest.getUserId();
			String openId = userOpenPlatformInfoAddRequest.getOpenId();
			Integer openPlatformId = userOpenPlatformInfoAddRequest.getOpenPlatformId();
			String openToken = userOpenPlatformInfoAddRequest.getOpenToken();
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("eqUserId", userId);
			param.put("eqOpenId", openId);
			int count = userOpenPlatformInfoDao.selectCountByMap(param);
			if (count > 0) {
				throw new ServiceOperationException("请求处理失败：已存在该开放平台的账号！");
			}
			UserOpenPlatformInfo userOpenPlatformInfo = new UserOpenPlatformInfo();
			userOpenPlatformInfo.setUserId(userId);
			userOpenPlatformInfo.setOpenId(openId);
			userOpenPlatformInfo.setOpenPlatformId(openPlatformId);
			userOpenPlatformInfo.setToken(openToken);
			userOpenPlatformInfoDao.insert(userOpenPlatformInfo);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	/**
	 * 查询用户开放平台信息
	 */
	@Override
	public Response getUserOpenPlatformList(Request request) throws Exception {
		PageListResponse<UserOpenPlatformInfo> response = new PageListResponse<UserOpenPlatformInfo>();
		try {
			UserOpenPlatformInfoListRequest userOpenPlatformInfoListRequest = (UserOpenPlatformInfoListRequest) request;
			Integer userId = userOpenPlatformInfoListRequest.getUserId();
			Integer openPlatformId = userOpenPlatformInfoListRequest.getOpenPlatformId();
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("eqUserId", userId);
			if (openPlatformId > 0) {
				param.put("eqOpenPlatformId", openPlatformId);
			}
			PageList<UserOpenPlatformInfo> list = userOpenPlatformInfoDao.selectByMap(param, false);
			response.setList(list);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
}
